import request from '@/utils/request'
import { authentication, dictionarySchema } from '@/configs'
import store from '@/store'

export function login (data) {
  if (authentication) {
    return request.post('/login', data)
  } else {
    return {
      code: 200,
      token: 123
    }
  }
}

export async function logout (data) {
  if (authentication) {
    const ssoSettings = JSON.parse(localStorage.getItem(dictionarySchema + '_ssoSettings') || '{}')
    if (ssoSettings.watfSso) {
      await request.post('/sso/logout')
    }
    if (ssoSettings.casSso) {
      const url = await request.get('/caslogout2')
      location.href = url
      localStorage.setItem('casLogoutUrl', url)
      store.dispatch('user/resetAll')
      return
    }
    return request.post('/logout', data)
  } else {
    return {
      code: 200
    }
  }
}

export async function getUserInfo () {
  if (authentication) {
    const data = await request.get('/userInfo')
    if (data && data.user) {
      data.user.userName = data.user.UNAME
    }
    return data
  } else {
    return {
      code: 200,
      user: {
        userName: '管理员'
      }
    }
  }
}
